package com.tenglong.teacher.ui.adapter.viewholder;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.netease.nim.uikit.business.session.constant.Extras;
import com.netease.nim.uikit.common.util.sys.TimeUtil;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.AnswerInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.adapter.AnswerAdapter;
import com.tenglong.teacher.ui.adapter.viewholder.base.BaseViewHolder;
import com.thinkcool.circletextimageview.CircleTextImageView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AnswerViewHolder extends BaseViewHolder<AnswerInfo> {
    private AnswerAdapter.AnswerListener listener;
    private CircleTextImageView headerImage;
    private TextView messageNum;
    private TextView name;
    private TextView className;
    private TextView howToDo;
    private ImageView image;
    private TextView state;
    private TextView time;
    private LinearLayout liulan_layout;
    private ImageView collect;
    private TextView shoucang;
    public AnswerViewHolder(View itemView) {
        super(itemView);
    }

    public AnswerViewHolder(View itemView, AnswerAdapter.AnswerListener listener){
        this(itemView);
        this.listener = listener;
        initView();
        setListener();
    }

    private void setListener() {
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onAnswerClick(getLayoutPosition());
                }
            }
        });
    }

    private void initView() {
        headerImage = itemView.findViewById(R.id.headerImage);
        messageNum = itemView.findViewById(R.id.messageNum);
        name = itemView.findViewById(R.id.name);
        className = itemView.findViewById(R.id.className);
        howToDo = itemView.findViewById(R.id.howToDo);
        image = itemView.findViewById(R.id.image);
        state = itemView.findViewById(R.id.state);
        time = itemView.findViewById(R.id.time);
        liulan_layout=itemView.findViewById(R.id.liulan_layout);
        collect=itemView.findViewById(R.id.collect);
        shoucang=itemView.findViewById(R.id.shoucang);
    }

    @Override
    public void setLayoutInfo(final AnswerInfo info) {
        RequestOptions options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.setting_image_defauft_header).
                placeholder(R.drawable.header_image_loading);
        Glide.with(itemView.getContext())
                .load(IpUtils.getImageUrl(info.stuPhotoUrl))
                .apply(options)
                .into(headerImage);
        name.setText(info.studentname);
        Glide.with(itemView.getContext())
                .load(IpUtils.getImageUrl(info.firstpicmsg))
                .apply(GlideOptions.getGlideRequestOptions().placeholder(R.drawable.answer_default_image)
                        .error(R.drawable.answer_default_image))
                .into(image);
        className.setText(info.classname);
        howToDo.setText(info.firsttextmsg);
        time.setText(TimeUtil.getFavoriteCollectTime(info.createDate));
        collect.setSelected(info.collect);
        shoucang.setText(" "+info.favorite);
        liulan_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                liulan_layout.setClickable(false);
                if(!collect.isSelected()){
                    info.favorite=info.favorite+1;
                }else{
                    info.favorite=info.favorite-1;
                }
                shoucang.setText(" "+info.favorite);
                collect.setSelected(!collect.isSelected());
                requestCollect(info,collect.isSelected());
            }
        });
        switch (info.statusid){
            case 1://未答疑
                state.setText("未答疑");
                state.setTextColor(ContextCompat.getColor(itemView.getContext(),R.color.app_red));
                state.setCompoundDrawablesWithIntrinsicBounds(R.drawable.weijieda, 0, 0, 0);
                break;
            case 2://答疑中
                state.setText("答疑中");
                state.setTextColor(Color.parseColor("#f50c33"));
                state.setCompoundDrawablesWithIntrinsicBounds(R.drawable.weijieda, 0, 0, 0);
                break;
            case 3://结束答疑
                state.setText("已解决");
                state.setTextColor(ContextCompat.getColor(itemView.getContext(),R.color.theme_green));
                state.setCompoundDrawablesWithIntrinsicBounds(R.drawable.yijieda, 0, 0, 0);
                break;
        }
    }
    private void requestCollect(final AnswerInfo info,final boolean isCollect) {
        CreateRequestEntity.getWebService().setCollectState(info.id, isCollect).enqueue(new Callback<BaseResult<String>>() {
            @Override
            public void onResponse(Call<BaseResult<String>> call, Response<BaseResult<String>> response) {
                liulan_layout.setClickable(true);
                if (!ResultVerifier.isSucceed(response)) {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                    collect.setSelected(!isCollect);
                    if(!isCollect){
                        shoucang.setText(""+(info.favorite+1));
                    }else{
                        shoucang.setText(""+(info.favorite-1));
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResult<String>> call, Throwable t) {
                liulan_layout.setClickable(true);
                ResultVerifier.systerErrers(t);
                collect.setSelected(!isCollect);
                if(!isCollect){
                    shoucang.setText(""+(info.favorite+1));
                }else{
                    shoucang.setText(""+(info.favorite-1));
                }
            }
        });
    }
}
